//
//  YLInteraciveWebTool.swift
//  YLIMKeyBoard
//
//  Created by AlbertYuan on 2023/2/3.
//

import Foundation
import WebKit
import SDBridgeSwift

public class YLInteraciveWebBridge:NSObject{

    public  var bridge: WebViewJavascriptBridge?
    public var webview: WKWebView?

    public init(webview: WKWebView) {
        super.init()

        bridge = WebViewJavascriptBridge(webView: webview)
        self.webview = webview

        registerDefaultNativeMethodForJS()
    }

    /// 注册默认的JS需要调用的原生方法
    public func registerDefaultNativeMethodForJS(){
        getSafeAreaHeight()

        webViewClose()

        webViewGoBack()
    }


    /// JS注册并调用原生
    /// - Parameters:
    ///   - nativeMethod: 协定的原生方法名称
    ///   - complete: JS需要执行的回调任务
    @objc public func registerCallNativeMethod(nativeMethod:String,
                                       complete:WebViewJavascriptBridgeBase.Handler? = nil){

       //js 传递过来的参数parameters只能是JSON格式
       bridge?.register(handlerName: nativeMethod) { (parameters, callback) in
           debugPrint("原生与JS交互",#function,nativeMethod,parameters as Any)
           complete?(parameters,callback)
       }
    }


    /// 原生同步、异步调用JS
    /// - Parameters:
    ///   - jsMethod: JS方法名称
    ///   - data: JS方法需要的参数
    ///   - complete: 原生回调处理
    @objc public func CallJSMethod(jsMethod:String,
                            data:Any,
                            complete:((_ responseData: Any) -> Void)? = nil){

        bridge?.call(handlerName: jsMethod, data: data) { responseData in
            debugPrint("原生与JS交互",#function,jsMethod,data,responseData as Any)

            guard let res = responseData else {
                debugPrint("Javascript console.log give native is nil")
                return
            }
            complete?(res)
        }
     }



}


// MARK: -- 通用注册方法
public extension YLInteraciveWebBridge{

    // MARK: -- //H5 获取安全区距离
    @objc func getSafeAreaHeight(){
        registerCallNativeMethod(nativeMethod:"getAppSafeArea",complete: { parameters, callback in
            let topSafe = UIDevice.xp_safeDistanceTop()
            let button =  UIDevice.xp_safeDistanceBottom()
            let res = ["topArea":topSafe,"butttonArea":button]
            callback?(res)
        })
    }


    // MARK: -- //H5 页面点击返回
    @objc func webViewGoBack(){
        registerCallNativeMethod(nativeMethod:"webViewGoBack",complete: { parameters, callback in
            let res = [:]
            if let webview = self.webview {
                //可以返回直接返回
                if webview.canGoBack{
                    webview.goBack()
                    callback?(res)
                    return
                }else{
                    //直接关闭
                    callback?(res)
                    k_currentVC.navigationController?.popViewController(animated: true)
                    return
                }
            }else{
                callback?(res)
                k_currentVC.navigationController?.popViewController(animated: true)
                return
            }
        })
    }


    // MARK: -- //H5 页面直接关闭
    @objc func webViewClose(){
        registerCallNativeMethod(nativeMethod:"webViewClose",complete: { parameters, callback in
            callback?([:])
            k_currentVC.navigationController?.popViewController(animated: true)
            return
        })
    }

}
